//
//  YWSubscripEquTypeTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 2017/5/17.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWSubscripEquTypeTableViewCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "YWEquipmentType.h"

@interface YWSubscripEquTypeTableViewCell()
@property (nonatomic, strong) UILabel * systemTypeLb;
@end

@implementation YWSubscripEquTypeTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setUpSubviews{
    _systemTypeLb.frame = CGRectMake(kMarginW, 0, SCREEN_WIDTH-2*kMarginW, 30);
    [self.contentView addSubview:_systemTypeLb];
}

- (UIButton *)newSelectNameButton{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.titleLabel.font = kContentFont;
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.layer.borderColor = [UIColor customTextGrayColor].CGColor;
    button.layer.borderWidth = 1.0;
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 15.0;
    [button addTarget:self action:@selector(handleCancelEvent:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (void)setEquipmentTypeArray:(NSMutableArray *)equipmentTypeArray{
    _equipmentTypeArray = equipmentTypeArray;
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.systemTypeLb.frame = CGRectMake(kMarginW, 0, SCREEN_WIDTH-2*kMarginW, 30);
    YWEquipmentType *EquType = _equipmentTypeArray[0];
    NSString *sysTypeNameStr = [NSString stringWithFormat:@"  %@",EquType.equipmentSystemType.systemTypeName];
    self.systemTypeLb.text = sysTypeNameStr;
    [self.contentView addSubview:_systemTypeLb];
    __block CGFloat w = 0;
    __block CGFloat h = 40;
    [_equipmentTypeArray enumerateObjectsUsingBlock:^(YWEquipmentType*  _Nonnull equipmentType, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton * button = [self newSelectNameButton];
        CGSize size =[equipmentType.equipmentTypeName sizeWithAttributes:@{NSFontAttributeName:kContentFont}];
        CGFloat length = size.width;
        button.frame = CGRectMake(10 + w, h, length + 15 , 30);
        if(10 + w + length + 15 + 10 > SCREEN_WIDTH){
            w = 0; //换行时将w置为0
            h = h + button.frame.size.height + 10;//距离父视图也变化
            button.frame = CGRectMake(10 + w, h, length + 20, 30);//重设button的frame
        }
        w = CGRectGetMaxX(button.frame);
        button.tag = 1000 + idx;
        [button setTitle:equipmentType.equipmentTypeName forState:UIControlStateNormal];
        [self.contentView addSubview:button];
    }];
}

- (UILabel*)systemTypeLb{
    if (!_systemTypeLb) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.backgroundColor = [UIColor customBackgroudColor];
        label.layer.masksToBounds = YES;
        label.layer.cornerRadius = 15.0;
        _systemTypeLb = label;
        [self.contentView addSubview:_systemTypeLb];
    }
    return _systemTypeLb;
}

- (void)handleCancelEvent:(UIButton*)sender{
    NSInteger tag = sender.tag-1000;
    YWEquipmentType * equType = [self.equipmentTypeArray objectAtIndex:tag];
    [self.equipmentTypeArray removeObjectAtIndex:tag];
    if ([self.delegate respondsToSelector:@selector(cancelBtnEventWithEquipmentType:withEquipmentType:)]) {
        [self.delegate cancelBtnEventWithEquipmentType:tag withEquipmentType:equType];
    }
}

@end
